package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {

    private MyBatisUtils(){
        
    }
    
    private  static SqlSessionFactory factory;
    
    // 静态代码块
    static {

        try {
            InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
            factory  = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    获取Sqlsession 对象

    */

    public static SqlSession getSqlSession(){
        return  factory.openSession(true);
    }
}
